package com.vanysoft.business.base.user.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.base.org.service.BaseOrgService;
import com.vanysoft.business.base.tag.service.BaseTagService;
import com.vanysoft.business.base.team.service.BaseTeamService;
import com.vanysoft.business.base.user.entity.TBaseUser;
import com.vanysoft.business.base.user.service.BaseUserService;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.utils.JSONResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目名称：SharpClaw移动门户
 * 类名称：BaseUserConditionController
 * 类描述：人员筛选器
 * 创建人：SharpClaw_x
 * 创建时间：2017年6月20日 下午1:52:27
 * 修改人：SharpClaw_x
 * 修改时间：2017年6月20日 下午1:52:27
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 */
@RestController
@RequestMapping("/base/userCondition")
public class BaseUserConditionController extends BaseController {
    @Autowired
    private BaseOrgService orgService;
    @Autowired
    private BaseTeamService teamService;
    @Autowired
    private BaseTagService BaseTagService;
    @Autowired
    private BaseUserService baseUserService;

    /**
     * Function Name               getCondition
     *
     * @return
     * @throws VanySoftBaseException
     * @description TODO 获取筛选添加配置
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月20日 上午10:12:14           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/getCondition", method = {RequestMethod.GET})
    public JSONResponse getCondition(@SessionUser UserInfo userInfo) throws VanySoftBaseException {
        //List<Map> resultList=baseUserService.getCondition(baseUserInfo.getUserId());
        List<Map> resultList = new ArrayList<>();
        //后期从数据库中配置读取
        Map<String, Object> baseMapCode = new HashMap<String, Object>();
        baseMapCode.put("label", "工号/姓名");
        baseMapCode.put("formname", "code");
        baseMapCode.put("type", "input");
        resultList.add(baseMapCode);

        Map<String, Object> baseMapGenger = new HashMap<String, Object>();
        baseMapGenger.put("label", "性别");
        baseMapGenger.put("formname", "gender");
        baseMapGenger.put("type", "select");
        baseMapGenger.put("options", getOptions());
        baseMapGenger.put("multiple", false);
        resultList.add(baseMapGenger);

        Map<String, Object> baseMapOrg = new HashMap<String, Object>();
        baseMapOrg.put("label", "部门");
        baseMapOrg.put("formname", "orgCode");
        baseMapOrg.put("type", "select");
        baseMapOrg.put("options", orgService.queryOrgTree(userInfo.getId()));
        baseMapOrg.put("multiple", false);
        baseMapOrg.put("istree", true);
        resultList.add(baseMapOrg);

//		Map<String,Object> baseMapTeam=new HashMap<String,Object>();
//		baseMapTeam.put("label", "群组");
//		baseMapTeam.put("formname", "teamCode");
//		baseMapTeam.put("type", "select");
//		baseMapTeam.put("options",teamService.loadMyTeams(baseUserInfo.getUserId()));
//		baseMapTeam.put("key",getTeamKey());
//		baseMapTeam.put("multiple",false);
//		resultList.add(baseMapTeam);
//		
//		Map<String,Object> baseMapTag=new HashMap<String,Object>();
//		baseMapTag.put("label", "分类");
//		baseMapTag.put("formname", "tagCode");
//		baseMapTag.put("type", "select");
//		baseMapTag.put("options",BaseTagService.loadTag("0", baseUserInfo.getUserId()));
//		baseMapTag.put("key",getTagKey());
//		baseMapTag.put("multiple",false);
//		baseMapTag.put("istree",true);
//		resultList.add(baseMapTag);

        return new JSONResponse(resultList);

    }


    private Map<String, Object> getTagKey() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", "tagCode");
        map.put("name", "tagName");
        map.put("children", "children");
        return map;
    }

    private Map<String, Object> getTeamKey() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", "teamCode");
        map.put("name", "teamName");
        //map.put("children","children");
        return map;
    }

    private List<Map> getOptions() {
        List<Map> sexList = new ArrayList<>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("text", "男");
        map.put("value", "1");
        sexList.add(map);

        Map<String, Object> mapN = new HashMap<String, Object>();
        mapN.put("text", "女");
        mapN.put("value", "0");
        sexList.add(mapN);

        return sexList;
    }

    /**
     * Function Name               getBaseUserInfo
     *
     * @param request
     * @return
     * @throws VanySoftBaseException
     * @description 筛选条件查找人员
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月20日 下午1:51:30           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/getBaseUserInfo", method = {RequestMethod.GET})
    public JSONResponse getBaseUserInfo(@SessionUser UserInfo userInfo, HttpServletRequest request) throws VanySoftBaseException {

        String code = request.getParameter("code");
        String gender = request.getParameter("gender");
        String orgCode = request.getParameter("orgCode");
        String teamCode = request.getParameter("teamCode");
        String tagCode = request.getParameter("tagCode");
        String pageIndex = request.getParameter("pageIndex");
        if (StringUtils.isEmpty(pageIndex)) {
            pageIndex = "1";
        }
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }

        Page<TBaseUser> resultList = baseUserService.getBaseUserInfo(userInfo.getId(), code, gender, orgCode,
                teamCode, tagCode, Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        return new JSONResponse(resultList);
    }

    /**
     * Function Name               getConditionAll
     *
     * @return
     * @throws VanySoftBaseException
     * @description (发送消息获取筛选添加配置 - 消息中心)
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年8月29日 下午2:40:58          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/getConditionAll", method = {RequestMethod.GET})
    public JSONResponse getConditionAll(@SessionUser UserInfo userInfo) throws VanySoftBaseException {

        List<Map> resultList = new ArrayList<>();

        //后期从数据库中配置读取
        Map<String, Object> baseMapCode = new HashMap<String, Object>();
        baseMapCode.put("label", "工号/姓名");
        baseMapCode.put("formname", "code");
        baseMapCode.put("type", "input");
        resultList.add(baseMapCode);

        Map<String, Object> baseMapGenger = new HashMap<String, Object>();
        baseMapGenger.put("label", "性别");
        baseMapGenger.put("formname", "gender");
        baseMapGenger.put("type", "select");
        baseMapGenger.put("options", getOptions());
        baseMapGenger.put("multiple", false);
        resultList.add(baseMapGenger);

        Map<String, Object> baseMapOrg = new HashMap<String, Object>();
        baseMapOrg.put("label", "部门");
        baseMapOrg.put("formname", "orgCode");
        baseMapOrg.put("type", "select");
        baseMapOrg.put("options", orgService.queryAllOrgList());
        baseMapOrg.put("multiple", false);
        baseMapOrg.put("istree", true);
        resultList.add(baseMapOrg);

        Map<String, Object> baseMapTeam = new HashMap<String, Object>();
        baseMapTeam.put("label", "群组");
        baseMapTeam.put("formname", "teamCode");
        baseMapTeam.put("type", "select");
        baseMapTeam.put("options", teamService.queryAllTeam());
        baseMapTeam.put("key", getTeamKey());
        baseMapTeam.put("multiple", false);
        resultList.add(baseMapTeam);

        Map<String, Object> baseMapTag = new HashMap<String, Object>();
        baseMapTag.put("label", "分类");
        baseMapTag.put("formname", "tagCode");
        baseMapTag.put("type", "select");
        baseMapTag.put("options", BaseTagService.loadAllTag());
        baseMapTag.put("key", getTagKey());
        baseMapTag.put("multiple", false);
        baseMapTag.put("istree", true);
        resultList.add(baseMapTag);

        return new JSONResponse(resultList);

    }

    /**
     * Function Name               getBaseUserAllInfo
     *
     * @param request
     * @return
     * @throws VanySoftBaseException
     * @description (发送消息查询所有人员 - 消息中心)
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年8月29日 下午2:10:02          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/getBaseUserAllInfo", method = {RequestMethod.GET})
    public JSONResponse getBaseUserAllInfo(@SessionUser UserInfo userInfo, HttpServletRequest request) throws VanySoftBaseException {

        String code = request.getParameter("code");
        String gender = request.getParameter("gender");
        String orgCode = request.getParameter("orgCode");
        String teamCode = request.getParameter("teamCode");
        String tagCode = request.getParameter("tagCode");
        String pageIndex = request.getParameter("pageIndex");
        if (StringUtils.isEmpty(pageIndex)) {
            pageIndex = "1";
        }
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }

        Page<Map<String, Object>> resultList = baseUserService.getBaseUserAllInfo(code, gender, orgCode,
                teamCode, tagCode, Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        return new JSONResponse(resultList);
    }
}
